<?php
namespace AppHome\Controller;
use Think\Controller;
class UploadController extends Controller {
    /*
     * curl_post 父类Controller的curl方法
     * */
    /*流程任务提交功能首页*/
    public function index(){
       $this->display();
    }

    public function info(){
       $data = I('param.');
       $this->display();
    }
    public function user(){
        $this->display();
    }
        public function signature(){
        $this->display();
    }
    /* 前往   对应客户身份上传操作*/
    /*TaskController/idsub*/
    public function idsub(){
        $img = $this->doupimgs();//图片上传 返回路径
        $data = I('param.');//前台提交的确认信息 
        $data['clientid'] = M('task')->where(array('taskid'=>$data['taskid']))->getField('clientid');
        //var_dump($_FILES);die;
        $data['img'] = $img;
        $data['time'] = date('Y/m/d H:i:s',time());
        //$data['protrait'] = 从身份证识别的肖像
        $m = M('check');
        $res = $m->where(array('taskid'=>$data['taskid']))->find();
        if($res){
            /*先上传的签字图片 修改记录*/
            $update = $m->where(array('taskid'=>$data['taskid']))->save($data);
            if($update){
               echo json_encode(array('status'=>1, 'mag'=>'添加成功'));
            }else{
               echo json_encode(array('status'=>0, 'msg'=>'添加失败'));
            }

        }else{
            /*没有该项记录 添加*/
            $add_result = $m->add($data);
            if($add_result){
                echo json_encode(array('status'=>1,'msg'=>'添加成功'));
            }else{
                echo json_encode(array('status'=>0,'msg'=>'添加失败'));
            }
        }
    }

/*对应 客户签名上传操作*/
    public function sign(){
       $image = $this->doupimgs();//上传图片的路径
       $data = I('post.');//taskid clientid 
       $data['clientid'] = M('task')->where(array('taskid'=>$data['taskid']))->getField('clientid');
       $data['sign'] = $image;
       $m = M('check');
       $res = $m->where(array('taskid'=>$data['taskid']))->find();
       if($res){
        /*验收员表有记录 修改*/
              $array['sign'] = $data['sign']; 
              $update = $m->where(array('taskid'=>$data['taskid']))->save($array);
              if($update){
                 echo json_encode(array('status'=>1,'msg'=>'上传成功'));
              }else{
                 echo json_encode(array('status'=>0, 'msg'=>'上传失败'));
              }

        }else{
            /*验收员表无记录 添加*/
              $add = $m->add($data);
              if($add){
                 echo json_encode(array('status'=>1, 'msg'=>'添加成功')); 
              } else{
                 echo json_encode(array('status'=>0, 'msg'=>'添加失败'));
              }
        }
    }

/*   油表里程图片信息入库   */
    public function submit(){
		
      $data['img'] = $this->base64(I('post.img'))['img_path'];
          //$data = I('post.');
  		$data['type'] = I('post.type');
  		$data['process'] = I('post.process');
  		$data['input'] = I('post.input');
  		$data['info'] = I('post.info 

');
  		$data['taskid'] = I('post.taskid');
      $data['userid'] = $_SESSION['user']['userid'];
     
      $data['time'] = date('Y-m-d',time());
      $m = M('upload');
      $res = $m->add($data);
      if($res){
        echo json_encode(array('status'=>1, 'msg'=>'操作成功'));
      }else{
        echo json_encode(array('status'=>0, 'msg'=>'操作失败'));
      }
      
    }

/*   车辆位置信息    */
/* 目的地坐标x y 距离目的地距离 当前坐标x y type*/
     public function position(){

       //$data = I('post.');//carid taskid addx addy type
       $data['taskid'] = I('post.taskid');
       //$data['type'] = I('post.type');
       $data['time'] = date('Y-m-d H:i:s',time());
       $data['userid'] = session('userid');
      // $data['userid'] = I('post.userid');//测试用
       /*当前位置*/
       $data['addx'] = I('post.curr_x');
       $data['addy'] = I('post.curr_y');
       /*目的地位置*/
       $dest_x = I('post.dest_x');
       $dest_y = I('post.dest_y');
       /*距离*/
       $diff = I('post.diff');

       $m = M('out_details');
       $res = $m->where(array('taskid'=>$data['taskid']))->find();
       $data['carid'] = M('task')->where(array('taskid'=>$data['taskid']))->getField('carid');
       if($res){
           
             /*修改记录*/  
              if(I('post.type')){
                 $data['type'] = I('post.type');
              } 
              $update = $m->where(array('taskid'=>$data['taskid']))->save($data);
              if($update){
                $type = $m->where(array('taskid'=>$data['taskid']))->getField('type');
                if($diff <= 100 && $type == 1){
                    $del = $m->where(array('taskid'=>$data['taskid']))->delete();
                    if($del){
                      echo json_encode(array('status'=>1, 'msg'=>'删除成功'));
                      die();
                    }else{
                      echo json_encode(array('status'=>0, 'msg'=>'删除失败'));
                      die();
                    }
                }else{
                    echo json_encode(array('status'=>1, 'msg'=>'更新位置信息成功'));
                    die();
                }
              }else{
                echo json_encode(array('status'=>0, 'msg'=>'更新位置信息失败'));
                die();
              }
         
       }else{
         /*添加记录*/
          $add_result = $m->add($data);
          if($add_result){
            echo json_encode(array('status'=>1, 'msg'=>'添加成功'));
          }else{
            echo json_encode(array('status'=>0, 'msg'=>'添加失败'));
          }
       }

     }

   

    /*接受图片验证后返回，可多次调用*/
    public function validate($url=''){
        $result = $this->doupimgs();
        echo json_encode(array('oil'=>array('path'=>'oil.jpg','status'=>'ok','volume'=>100)));
        exit;
        /*调用API的URL地址*/
        $curl_data = $this->curl_post($url,$result);
        echo $curl_data;
    }
    /*用户上传签名照
    public function user_photo(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      'Upload/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $results[$file['key']]= 'z/Upload/'.$file['savepath'].$file['savename'];
            }
        }
    }*/
/*public function sub(){
        $image = $this->doupimgs();//路径
        $data = I('post.');//
        $data['img'] = $image;
        $m = M('upload');
        $map['id'] = $data['id'];
        //先添加上传记录后更新原始任务记录表
        $add_result = $m->add($data);
        $update_result = $m->where($map)->save($data);
        if($update_result){
            echo json_encode(array('status'=>1,'msg'=>'添加成功'));
        }
    }
*/
    
}